<template>
  <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false" @close="closeModal">
    <el-form ref="form" :model="form" :show-message="false" label-width="100px">
      <el-form-item label="服务商" prop="companyId" required>
        <SelectCompany
          style="width: 90%;"
          :defaultValue.sync="form.companyId"
          :companyType="1"
          placeholder="请选择服务商"
          isAll
        />
        <!-- <el-select v-model="form.companyId" placeholder="请选择服务商" clearable filterable style="width: 90%;">
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.cname"
            :value="item.id"
          ></el-option>
        </el-select> -->
        <div class="color9">邀请发出后，待对方审核通过后双方达成合作。</div>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确认邀请</el-button>
      <el-button @click="open = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addApplyCooperate, getServiceCompanyList } from '@/api/facilitator'
import SelectCompany from "@/components/SelectCompany";

export default {
  components: {
    SelectCompany
  },
  data() {
    return {
      title: "邀请TA成为我的服务商",
      open: false,
      form: {
        companyId: undefined
      },
      options: []
    };
  },
  methods: {
    openModal() {
      this.open = true;
      // 获取当前登录账号事业部下的所有服务商
      getServiceCompanyList({pageSize: 9999}).then(res=>{
        this.options = res.rows;
      })
    },
    closeModal() {
      this.$refs["form"].resetFields();
      Object.assign(this.$data, this.$options.data()); // 存在自定义校验规则不能重置data
    },
    submitForm() {
      const that = this;
      that.$refs["form"].validate(async (valid) => {
        if (valid) {
          await addApplyCooperate(this.form);
          that.$message.success("已发送邀请，待对方审核");
          that.$emit("refresh");
          that.closeModal();
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped></style>
